﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace SharpSoft.WPF.Validations
{
    /// <summary>
    /// 字符串验证规则
    /// </summary>
    public class StringValidationRule : ValueValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            var vr = base.Validate(value, cultureInfo);
            if (vr != Uncertainty)
            {
                return vr;
            }
            var str = value.ToString();
            if (!AllowEmpty && str == "")
            {
                return new ValidationResult(false, EmptyError);
            }
            if (!AllowWhitespace)
            {
                foreach (var item in str)
                {
                    if (char.IsWhiteSpace(item))
                    {
                        return new ValidationResult(false, WhitespaceError);
                    }
                }
            }
            if (MinLength.HasValue && str.Length < MinLength.Value)
            {
                return new ValidationResult(false, string.Format(LessThanMinLengthError, MinLength));
            }
            if (MaxLength.HasValue && str.Length > MaxLength.Value)
            {
                return new ValidationResult(false, string.Format(GreaterThanMaxLengthError, MaxLength));
            }
            if (!string.IsNullOrEmpty(Regex))
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(this.Regex, System.Text.RegularExpressions.RegexOptions.ECMAScript);
                if (!regex.IsMatch(str))
                {
                    return new ValidationResult(false, RegexNoMatchError);
                }
            }
            return ValidationResult.ValidResult;
        }
        /// <summary>
        /// 是否允许为空白字符
        /// </summary>
        public bool AllowEmpty { get; set; }
        /// <summary>
        /// 当为空字符串时的错误消息
        /// </summary>
        public string EmptyError { get; set; } = "必须填写";

        /// <summary>
        /// 是否允许空白字符
        /// </summary>
        public bool AllowWhitespace { get; set; } = true;
        /// <summary>
        /// 当出现空白字符时的错误消息
        /// </summary>
        public string WhitespaceError { get; set; } = "不允许出现空白字符";

        /// <summary>
        /// 最小长度
        /// </summary>
        public int? MinLength { get; set; }
        /// <summary>
        /// 当字符数量低于下限时的错误消息
        /// </summary>
        public string LessThanMinLengthError { get; set; } = "不能少于{0}个字符";
        /// <summary>
        /// 最大长度
        /// </summary>
        public int? MaxLength { get; set; }
        /// <summary>
        /// 当字符数量超出上限时的错误消息
        /// </summary>
        public string GreaterThanMaxLengthError { get; set; } = "不能超过{0}个字符";
        /// <summary>
        /// 正则限定
        /// </summary>
        public string Regex { get; set; }
        /// <summary>
        /// 当正则不匹配时的错误消息
        /// </summary>
        public string RegexNoMatchError { get; set; } = "不符合规则";
    }
}
